package es.efor.ics.gev.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Version;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * The persistent class for the <b>MATERIALES</b> database table.
 * 
 * @author dlisbona
 *
 */
@Entity
@Table(name = "MATERIALES")
public class Material extends BaseObject implements Serializable {

	// static data
	private static final long serialVersionUID = -2172803327553089755L;

	// private instance data
	private Long id;
	private String nombreMaterial;
	private Set<Clase> clases = new HashSet<Clase>();
	private Integer version;

	/**
	 * Construct an uninitialized <code>Material</code> (for runtime proxy
	 * generation in Hibernate).
	 */
	public Material() {
	}

	/**
	 * Construct an initialized <code>Material</code>.
	 * 
	 * @param nombreMaterial
	 */
	public Material(String nombreMaterial) {
		this.nombreMaterial = nombreMaterial;
	}

	// ********************** Accessor Methods ********************** //

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "MATERIAL_ID")
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "NOMBRE_MATERIAL", length = 255, nullable = false)
	public String getNombreMaterial() {
		return nombreMaterial;
	}

	public void setNombreMaterial(String nombreMaterial) {
		this.nombreMaterial = nombreMaterial;
	}

	@ManyToMany(mappedBy = "materiales", fetch = FetchType.LAZY)	
	@org.hibernate.annotations.LazyCollection(value = org.hibernate.annotations.LazyCollectionOption.TRUE)
	public Set<Clase> getClases() {
		return clases;
	}

	public void setClases(Set<Clase> clases) {
		this.clases = clases;
	}

	@Version
	@Column(name = "OBJ_VERSION")
	public Integer getVersion() {
		return version;
	}	

	public void setVersion(Integer version) {
		this.version = version;
	}

	// ********************** Common Methods ********************** //

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Material == false) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		Material material = (Material) obj;
		return new EqualsBuilder().append(this.getNombreMaterial(),
				material.getNombreMaterial()).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(this.getNombreMaterial())
				.toHashCode();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).append(
				"nombreMaterial", this.getNombreMaterial()).toString();
	}

}
